package utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import pojo.entity.Good;

import java.io.IOException;
import java.util.ArrayList;

public class GetGoods {

    String path1="https://search.jd.com/Search?keyword=";
    String path2="&enc=utf-8&wq=";
    String path3="&s=1&click=1&page=";

    Good good;
    ArrayList arrayList=new ArrayList();

    public ArrayList<Good> getMassage(String test, String page) {

        String url=path1+test+path2+test+path3+page;
        try {
            Document document = Jsoup.connect(url).get();

            Elements elements = document.getElementsByClass("gl-item");
            System.out.println(elements.size());
            System.out.println("-----------------------------------------");
            for (Element element : elements) {
                //获取连接和图片
                Elements elementsByClass = element.getElementsByClass("p-img");
                //获取详情页连接
                Elements hrefA = elementsByClass.get(0).getElementsByTag("a");
                String href = hrefA.attr("href");
                //获取img路径
                Elements imgClass = elementsByClass.get(0).getElementsByTag("img");
                String img = imgClass.attr("data-lazy-img");

                /*---------------------------------------------*/

                Elements priceClass = element.getElementsByClass("p-price");
                Elements priceI = priceClass.get(0).getElementsByTag("i");
                String priceString = priceI.get(0).text();
                Double price=Double.valueOf(priceString);

                /*---------------------------------------------*/

                Elements nameClass = element.getElementsByClass("p-name");
                Elements nameEm = nameClass.get(0).getElementsByTag("em");
                String name = nameEm.get(0).text();

                /*---------------------------------------------*/

                element.getElementsByClass("p-commit");

                /*---------------------------------------------*/

                Elements shopClass = element.getElementsByClass("p-shop");
                Elements shopA = shopClass.get(0).getElementsByTag("a");
                String shop = shopA.get(0).text();

                /*---------------------------------------------*/

                String icon="";
                try {
                    Elements iconsClass = element.getElementsByClass("p-icons");
                    Elements iconI = iconsClass.get(0).getElementsByTag("i");
                    icon = iconI.get(0).text();
                }catch (Exception e){
                    icon="-";
                }
                good=new Good(href,name,price,shop,img,icon);
//                System.out.println(good);
                arrayList.add(good);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return arrayList;
    }

    public static void main(String[] args) {
        GetGoods getGoods=new GetGoods();
        getGoods.getMassage("手机","1");
    }

}
